package org.gitos.cloud.background.controller;

import javax.servlet.http.HttpServletRequest;

import org.gitos.cloud.cmd.controller.BaseController;
import org.gitos.cloud.domain.annotation.UnAuth;
import org.gitos.cloud.domain.comm.BConstant;
import org.gitos.cloud.domain.rpc.BLoginRpc;
import org.gitos.cloud.util.JSession;
import org.gitos.cloud.util.RedisHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.dubbo.config.annotation.Reference;

@Controller
public class BLoginController extends BaseController {
	private static final Logger log = LoggerFactory.getLogger(BLoginController.class);

	@Reference
	private BLoginRpc bLoginRpc;

	/* 操作类请求 */
	@UnAuth
	@RequestMapping("/background/login")
	public String login(@RequestParam("username") String username, @RequestParam(name = "password") String password,
			Model model, HttpServletRequest request) {
		String jsession_id = request.getAttribute(BConstant.JSESSION_ID_REQUEST).toString();
		boolean equal = bLoginRpc.isPasswordEqual(username, password);
		if (equal) {
			JSession jsession = RedisHelper.getJSession(jsession_id);
			jsession.setData(bLoginRpc.get(username));
			System.out.println("登录成功新缓存为：" + RedisHelper.get(jsession_id));
			return "index";
		}
		model.addAttribute("err_msg", "用户名或密码错误！");
		return "login";
	}

	@UnAuth
	@RequestMapping("/background/loginWeb")
	public String loginWeb() {
		return "login";
	}

	@UnAuth
	@RequestMapping("/background/loginOut")
	public String loginOut(HttpServletRequest request) {
		RedisHelper.delete(request.getAttribute(BConstant.JSESSION_ID_REQUEST).toString());
		return "login";
	}

}
